package both;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

/**
 * 2 June 2014 v1.0
 * @author mdale8127
 */
public class StringFormatter {
    public static final int TITLE_STR = 1;
    public static final int QUESTION_STR = 2;
    public static final int ANSWER_STR = 3;
    public static final int TAB_STR = 5;
    public static final int BUTTON_STR = 6;
    public static final int ANSWER_SIZE = 5;
    public static final int QUESTION_SIZE = 6;
    public static final int TITLE_SIZE = 7;
    public static final int TAB_SIZE = 4;
    public static final int BUTTON_SIZE = 3;
    private static final String frmtStr1 = "<html><font size=";
    private static final String frmtStr2 = ">";
    private static final String frmtStr3 = "</font></html>";
    
    public static String formatString(String oldStr, int type, int width){
        String newStr = frmtStr1;
        newStr += size(type);
        newStr += frmtStr2 + oldStr + frmtStr3;
        return String.format("<html><div WIDTH=%d>%s</div></html>", width, newStr);
    }
    
    public static String formatString(String oldStr, int type){
        int width = 0;
        Font f = UIManager.getDefaults().getFont("TabbedPane.font");
        int screenRes = Toolkit.getDefaultToolkit().getScreenResolution();
        int fontSize = (int)Math.round(size(type) * screenRes / 72.0);
        f = new Font(f.getName(), f.getStyle(), fontSize);
        BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
        FontMetrics fm = img.getGraphics().getFontMetrics(f);
        int n = oldStr.split("\\s").length;
        width += fm.stringWidth(" ") * 3 * size(type) * (n + 1);
        width += fm.stringWidth(oldStr);
        return formatString(oldStr, type, width);
    }
    
    private static int size(int type){
        if(type == TITLE_STR){
            return TITLE_SIZE;
        } else if(type == QUESTION_STR){
            return QUESTION_SIZE;
        }else if(type == TAB_STR){
            return TAB_SIZE;
        }else if(type==ANSWER_STR){
            return ANSWER_SIZE;
        } else {
            return BUTTON_SIZE;
        }
    }
}
